package com.tm.controller;

import com.tm.utils.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @PROJECT_NAME: tm-wm-syb
 * @DESCRIPTION:
 * @USER: DELL
 * @DATE: 2023/4/3 19:12
 */
@RestController
@RequestMapping("common")
public class CommonController {

    @Value("${file.path}")
      private String path;
@PostMapping("upload")
    public R upload(@RequestBody MultipartFile file){
        //获取原始文件名称
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀名的其实下标
        int i = originalFilename.lastIndexOf(".");
        //文件后缀
        String suffix = originalFilename.substring(i);
            //新的文件名
        String newFileName = UUID.randomUUID().toString().replace("-","") +suffix;

        File newFile = new File(path);
        //判断这个文件夹是否存在
        if (!newFile.exists()){
            newFile.mkdir();
        }
        File newFile2 = new File(path,newFileName);
        try {
            file.transferTo(newFile2);
        } catch (IOException e) {
                throw new RuntimeException(e);
        }
     return R.success(newFileName);

    }
}
